﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;
using PagedList;

namespace ShenLanCMS.Web.Controllers
{
    public class SearchController : Controller
    {
        private EntityContext db = new EntityContext();

        int pageSize = 10;

        public ViewResult Index(int? page)
        {
            var skey = Request.QueryString["keyword"];
            ViewData["keyword"] = skey;
            ViewData["actionName"] = "Index";
            var index = (page ?? 1) - 1;

            var post = db.Posts.Where(n => n.Title.Contains(skey) || n.Content.Contains(skey));

            if (!string.IsNullOrEmpty(skey))
                ViewData["result"] = skey + " 的搜索结果共有" + post.Count() + "条";

            return View(post.ToPagedList(index, pageSize));
        }
 
        //public ViewResult Details(Guid id)
        //{
        //    Search search = db.Searches.Find(id);
        //    return View(search);
        //}

 
        //public ActionResult Create()
        //{
        //    return View();
        //}
 
        //[HttpPost]
        //public ActionResult Create(Search search)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        search.SearchID = Guid.NewGuid();
        //        db.Searches.Add(search);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(search);
        //}
 
        //public ActionResult Edit(Guid id)
        //{
        //    Search search = db.Searches.Find(id);
        //    return View(search);
        //}
 
        //[HttpPost]
        //public ActionResult Edit(Search search)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(search).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(search);
        //}
 
        //public ActionResult Delete(Guid id)
        //{
        //    Search search = db.Searches.Find(id);
        //    return View(search);
        //}
 
        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(Guid id)
        //{
        //    Search search = db.Searches.Find(id);
        //    db.Searches.Remove(search);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}